<?php

if (!isset($_SESSION)) {
	session_start();
}

require_once 'class/config.php';

class InsertInfo
{
	private $db, $chooseContent;
	private $cTreaties_cTreadyID, $cTreaties_treatyID, $cTreaties_countryID, 
		$cTreaties_signature, $cTreaties_ratification, $cTreaties_details;
	private $chapters_chapterID, $chapters_chapterNo, $chapters_chapterName;
	private $countries_countryID, $countries_countryName;
	private $treaties_treatyID, $treaties_chapterID, $treaties_title, $treaties_conclusionDate,
		$treaties_conclusionPlace, $treaties_EFDate, $treaties_EFText, $treaties_registrationDate,
		$treaties_registrationNo, $treaties_treatyText, $treaties_fullTreatyText, $treaties_note;
	
	public function __construct()
	{
		$this->db = DataBase::getInstance();
		$this->chooseContent = new ChooseContent();
	}
	
	function searchFiles()
	{
		
	}
	
	function getID()
	{
		$id = $this->db->getID();
	}
	
	function insertChapter($chapterNo, $chapterName)
	{
		$checkChapter = $this->db->getChapter($chapterNo, $chapterName);
		if ($checkChapter) {
			$chapterID = $checkChapter['chapterID'];
		} else {
			$this->db->insertChapter($chapterNo, $chapterName);
			$chapter = $this->db->getID();
			$chapterID = $chapter["LAST_INSERT_ID()"];
		}
		return $chapterID;
	}
	
	function insertTreaty($chapterID, $title, $conclusionDate, $conclusionPlace, $EFDate, 
		$EFText, $registrationDate, $registrationNo, $treatyText, $fullTreatyText, $note)
	{
		$checkTreaty = $this->db->checkTreaty($title);
		if ($checkTreaty) {
			$treatyID = $checkTreaty['treatyID'];
		} else {
			$this->db->insertTreaty($chapterID, $title, $conclusionDate, $conclusionPlace, $EFDate,
				$EFText, $registrationDate, $registrationNo, $treatyText, $fullTreatyText, $note);
			$treaty = $this->db->getID();
			$treatyID = $treaty["LAST_INSERT_ID()"];
		}
		return $treatyID;
	}
	
	function insertCountry($countryName, $countryIdentifiers)
	{
		if ($countryName == "Côte d'Ivoire"){
			$countryName = "Cote d'Ivoire";
		}
		$checkCountry = $this->db->getCountry($countryName);
		if ($checkCountry) {
			$countryID = $checkCountry['countryID'];
		} else {
			$countryIdentifier = $this->chooseContent->getCountryIdentifier($countryIdentifiers, $countryName);
			$this->db->insertCountry($countryName, $countryIdentifier);
			$country = $this->db->getID();
			$countryID = $country["LAST_INSERT_ID()"];
		}
		return $countryID;
	}
	
	function insertCTreaty($treatyID, $countryID, $signature, $ratification, $details)
	{
		$checkCTreaty = $this->db->checkCTreaty($treatyID, $countryID);
		if ($checkCTreaty['cTreatyID']) {
			if ($checkCTreaty['signature'] != $signature ||
				$checkCTreaty['ratification'] != $ratification ||
				$checkCTreaty['details'] != $details) {
					$this->db->updateCTreaty($checkCTreaty['cTreatyID'], $signature, $ratification, $details);
				}	
			$cTreatyID = $checkCTreaty['cTreatyID'];		
		} else {
			$this->db->insertCTreaty($treatyID, $countryID, $signature, $ratification, $details);
			$cTreaty = $this->db->getID();
			$cTreatyID = $cTreaty["LAST_INSERT_ID()"];
		}
		return $cTreatyID;
	}
}